package com.LeeCode;

/**
 * 删掉一个元素以后全为 1 的最长子数组
 */

public class Code1493 {
    public static void main(String[] args) {
        int[] nums = {1, 1, 0, 1};
        System.out.println(new Code1493().longestSubarray(nums));
    }

    public int longestSubarray(int[] nums) {
        int ans = 0, left = 0, count = 0;
        int n = nums.length;

        for (int i = 0; i < n; i++) {
            if (nums[i] == 0)
                count++;

            while (count > 1) {
                if (nums[left] == 0)
                    count--;
                left++;
            }

            ans = Math.max(ans, i - left + 1 - count);
        }
        return count == 1 ? ans : ans - 1;
    }
}
